////
//
/// \file   ./src/block_entry_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/block_entry_node.hpp>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/parse_tree_node.hpp>
#include <inc/parser/statement_node.hpp>
#include <inc/parser/if_statement_node.hpp>
#include <inc/parser/local_decl_node.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;

/// \brief Basic constructor for a block entry node
/// \author Chandler Carruth
/// \date 2008.10.04

BlockEntryNode::BlockEntryNode()
{}

/// \brief Empty destructor, all members are smart pointers
/// \author Chandler Carruth
/// \date 2008.10.04

BlockEntryNode::~BlockEntryNode()
{}

/// \brief  Parse a block entry node
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This parses an entry within an INC block. It selects between executed
/// statements, conditional constructs, looping constructs, and scope
/// constructs.

bool
BlockEntryNode::parse( const iterator &first,
                       const iterator &last,
                       iterator &last_parsed )
{
  assert( get_text().empty() && !node
          && "This node was parsed more than once!" );

  //  Attempt to parse a variable declaration.
  scoped_ptr<ParseTreeNode> decl( new LocalDeclNode );
  if( decl->parse( first, last, last_parsed ) )
  {
    set_text( decl->get_text() );
    node.swap( decl );
    return true;
  }

  //  Attempting to parse an if-statement node.
  scoped_ptr<ParseTreeNode> if_stmt( new IfStatementNode );
  if( if_stmt->parse( first, last, last_parsed ) )
  {
    set_text( if_stmt->get_text() );
    node.swap( if_stmt );
    return true;
  }

  //  Attempting to parse a statement node. We do this parse last because all
  //  other block entries begin with a reserved word, and thus will fail fast.
  //  This parse can be much slower.
  scoped_ptr<ParseTreeNode> stmt( new StatementNode );
  if( stmt->parse( first, last, last_parsed ) )
  {
    set_text( stmt->get_text() );
    node.swap( stmt );
    return true;
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief  Convenience getter for a Statement sub-node
/// \author Chandler Carruth
/// \date   2008.05.16

StatementNode*
BlockEntryNode::get_statement() const
{
  return dynamic_cast<StatementNode*>( node.get() );
}

/// \brief  Convenience getter for an IfStatement sub-node
/// \author Chandler Carruth
/// \date   2008.10.02

IfStatementNode*
BlockEntryNode::get_if_statement() const
{
  return dynamic_cast<IfStatementNode*>( node.get() );
}

/// \brief  Convenience getter for a variable declaration sub-node
/// \author Chandler Carruth
/// \date   2008.10.04

LocalDeclNode*
BlockEntryNode::get_local_decl() const
{
  return dynamic_cast<LocalDeclNode*>( node.get() );
}

} //  end parser namespace
} //  end inc namespace
